home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / raytrace / renaisnc / lib.lha / Lib / meshsym.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-04  |  2.9 KB  |  150 lines

  1. /* File: meshsym.c
  2. ** Author: Charles Loop
  3. ** Purpose: Symbol and Edge table routines required for the creation of a mesh
  4. ** Last Modified: 2 July, 1987
  5. ** Reason : Creation.
  6. */
  7.  
  8. #include <stdio.h>
  9. #include "Math.h"
  10. #include "4D.h"
  11. #include "mesh.h"
  12.  
  13. #define SYMBOL_HASH_SIZE 167
  14. #define EDGE_HASH_SIZE 257
  15.  
  16. static SymbolEntry *SymbolTable[SYMBOL_HASH_SIZE];
  17. static EdgeEntry *EdgeTable[EDGE_HASH_SIZE];
  18.  
  19. SymbolHash(s)
  20. char *s;
  21. {
  22.         char *p;
  23.     unsigned h = 0, g;
  24.  
  25.     for (p = s; *p != '\0'; p++) {
  26.             h = (h << 4) + (*p);
  27.         if (g = h&0xf0000000) {
  28.                 h = h ^ (g >> 24);
  29.             h = h ^ g;
  30.         }
  31.     }
  32.     return h % SYMBOL_HASH_SIZE;
  33. }
  34.  
  35. SymbolEntry *LookupSymbol(s)
  36.         char *s;
  37. {
  38.         SymbolEntry *sp;
  39.  
  40.         for (sp = SymbolTable[SymbolHash(s)]; 
  41.          sp != (SymbolEntry *) 0; sp = sp->next)
  42.             if (strcmp(sp->name, s) == 0)
  43.                 return sp;
  44.         return (SymbolEntry *) 0;
  45. }
  46.  
  47. SymbolEntry *InstallSymbol(s)
  48.         char  *s;
  49. {
  50.         SymbolEntry *sp;
  51.     int    hashval;
  52.  
  53.     if ((sp = LookupSymbol(s)) == (SymbolEntry *) 0) {
  54.             sp = (SymbolEntry *) malloc(sizeof(SymbolEntry));
  55.         sp->name = (char *) malloc(sizeof(char)*(strlen(s)+1));
  56.         strcpy(sp->name, s);
  57.         hashval = SymbolHash(sp->name);
  58.         sp->next = SymbolTable[hashval];
  59.         SymbolTable[hashval] = sp;
  60.     }
  61.         return sp;
  62. }
  63.  
  64. FreeSymbol()
  65. {
  66.   SymbolEntry *sp, *tp;
  67.   int i;
  68.  
  69.   for (i = 0; i < SYMBOL_HASH_SIZE; i++) {
  70.     for (sp = SymbolTable[i]; sp != (SymbolEntry *) 0;) {
  71.       tp = sp;
  72.       sp = sp->next;
  73.       free((char *) tp);
  74.     }
  75.     SymbolTable[i] =(SymbolEntry *) 0;
  76.   }
  77. }
  78.       
  79. EdgeHash(p,q)
  80.         PointType4D *p,*q;
  81. {
  82.         return (((unsigned int)p + (unsigned int)q) % EDGE_HASH_SIZE);
  83. }
  84.  
  85. Edge *LookupEdge(p,q)
  86.         PointType4D *p,*q;
  87. {
  88.         EdgeEntry *ep;
  89.  
  90.     for (ep = EdgeTable[EdgeHash(p,q)];
  91.          ep != (EdgeEntry *) 0; ep = ep->next)
  92.             if (ep->p == p && ep->q == q)  /* entry found */
  93.                 return ep->e;
  94.  
  95.     return (Edge *) 0;
  96. }
  97.  
  98. InstallEdge(p,newedge)
  99.         PointType4D *p;
  100.     Edge  *newedge;
  101. {
  102.         EdgeEntry  *ep;
  103.     Edge       *oldedge;
  104.     PointType4D      *q = newedge->p;
  105.     int        hashval;
  106.  
  107.     if (oldedge = LookupEdge(q,p)) {
  108.             if (oldedge->e)
  109.                 fprintf(stderr, "edge usage conflict\n");
  110.  
  111.         newedge->e = oldedge;
  112.         oldedge->e = newedge;
  113.         }
  114.     else {
  115.             if (LookupEdge(p,q))
  116.                 fprintf(stderr, "edge orientation conflict\n");
  117.  
  118.         ep = (EdgeEntry *) malloc(sizeof(EdgeEntry));
  119.         ep->p = p;
  120.         ep->q = q;
  121.         ep->e = newedge;
  122.         hashval = EdgeHash(p,q);
  123.         ep->next = EdgeTable[hashval];
  124.         EdgeTable[hashval] = ep;
  125.     }
  126. }
  127.  
  128. FreeEdge()
  129. {
  130.   EdgeEntry *ep, *tp;
  131.   int i;
  132.  
  133.   for (i = 0; i < EDGE_HASH_SIZE; i++ ) {
  134.     for (ep = EdgeTable[i]; ep != (EdgeEntry *) 0;) {
  135.       if (ep->e) {
  136.     if (ep->e->e)
  137.       free((char *) ep->e->e);
  138.     free((char *) ep->e);
  139.       }
  140.       tp = ep;
  141.       ep = ep->next;
  142.       free((char *) tp);
  143.     }
  144.     EdgeTable[i] = (EdgeEntry *) 0;
  145.   }
  146.  
  147. }
  148.  
  149.  
  150.